﻿using Serilog;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;

namespace WatchDog
{
    public class ProcessHelper
    {
        /// <summary>
        /// 判断指定的程序是否正在运行
        /// </summary>
        /// <param name="fullPath"></param>
        /// <returns></returns>
        public static bool IsRuning(string fullPath)
        {
            try
            {
                var process = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(fullPath));
                return process.Any(c => c.MainModule.FileName == fullPath);
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 获取程序运行时长（分钟）
        /// </summary>
        /// <param name="fullPath"></param>
        /// <returns></returns>
        public static double GetRunningTotalMinutes(string fullPath)
        {
            try
            {
                var process = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(fullPath));
                var m = process.FirstOrDefault(c => c.MainModule.FileName == fullPath);
                if (m != default)
                {
                    return (DateTime.Now - m.StartTime).TotalMinutes;
                }
                return 0;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 启动指定的程序
        /// </summary>
        /// <param name="fullPath"></param>
        public static void Start(string fullPath)
        {


            try
            {
                if (Environment.UserInteractive)
                {
                    var proc = new Process();
                    proc.StartInfo.FileName = Path.GetFileName(fullPath);
                    proc.StartInfo.WorkingDirectory = Path.GetDirectoryName(fullPath);
                    //proc.StartInfo.Verb = "runas";
                    //proc.StartInfo.UseShellExecute = false;
                    //proc.StartInfo.CreateNoWindow = true;
                    proc.Start();
                }
                else
                {
                    Windows服务启动GUI进程.Start(fullPath, true);
                }
            }
            catch (Exception ex)
            {
                Log.Logger.Error(ex, $"启动{fullPath}时出错");
            }
        }

        /// <summary>
        /// 停止指定的程序
        /// </summary>
        /// <param name="fullPath"></param>
        public static void Stop(string fullPath)
        {
            try
            {
                var process = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(fullPath))
                    .Where(c => c.MainModule.FileName == fullPath);

                if (process.Any())
                {
                    foreach (var item in process)
                    {
                        item.Kill();
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Logger.Error(ex, $"结束{fullPath}时出错");
            }
        }
    }
}
